package com.yangyang.dingnote;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteViewHolder> {

    private List<Note> notes;
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(Note note);

        void onItemLongClick(Note note);
    }

    public NoteAdapter(List<Note> notes, OnItemClickListener listener) {
        this.notes = notes;
        this.listener = listener;
    }

    @NonNull
    @Override
    public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_note, parent, false);
        return new NoteViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull NoteViewHolder holder, int position) {
        Note note = notes.get(position);
        holder.noteTitle.setText(note.getTitle());
        holder.noteContent.setText(note.getContent());
        holder.itemView.setOnClickListener(v -> listener.onItemClick(note));
        holder.itemView.setOnLongClickListener(v -> {
            listener.onItemLongClick(note);
            return true;
        });
    }

    @Override
    public int getItemCount() {
        return notes.size();
    }

    static class NoteViewHolder extends RecyclerView.ViewHolder {
        TextView noteTitle;
        TextView noteContent;

        public NoteViewHolder(@NonNull View itemView) {
            super(itemView);
            noteTitle = itemView.findViewById(R.id.noteTitle);
            noteContent = itemView.findViewById(R.id.noteContent);
        }
    }


    // 获取 Note ID
    public Long getNoteId(int position) {
        Note note = notes.get(position);
        return note.getId();
    }

    // 新增方法：删除笔记
    public void deleteNote(int position) {
        notes.remove(position);
        notifyItemRemoved(position); // 触发删除动画
    }

    // 新增方法：归档笔记（示例，具体逻辑根据需求调整）
//    public void archiveNote(int position) {
//        Note note = notes.get(position);
//        // TODO: 调用数据库方法归档笔记
//        notes.remove(position);
//        notifyItemRemoved(position);
//    }


    // 归档 Note
    public void archiveNote(int position) {
        Note note = notes.get(position);
        //note.setArchived(true); // 假设 Note 类有 `setArchived` 方法
        notifyItemChanged(position); // 更新 UI (修改内容 不移除)
    }
}